// pages/home/home.js
import { request } from '../../api/api'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    gdata: app.globalData,
    msg: "WH2214-yyds",
    count: 2000,
    word: 'Are you oK',
    links: 'https://img2.baidu.com/it/u=3668201019,2476631369&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1673456400&t=22f29f09df98d60b1b9cfe354e85f83e',
    flag: true,
    links2: 'https://img2.baidu.com/it/u=1835843610,1575206394&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1673456400&t=15ea7a46bf6c976379b3d13c900e1dc9',
    nums: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    course: ['Vue2', "Vue3", "React", "Node", "小程序"],
    banners: [],
    goods: [],
    pageNo: 1,
    pageSize: 10,
    total: 0
  },

  refreshdata() {
    console.log('refreshdata')
  },
  loadmore() {
    console.log('loadmore')
  },
  scrollaction() {
    console.log('scrollaction')
  },
  async getgoodsData() {
    let res = await request({
      url: '/react/getgoods',
      data: {
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize,
      }
    })
    this.setData({
      goods: res.data.result,
      total: res.data.total,
    })
    console.log(res)
  },
  async getbanners() {
    let res = await request({
      url: '/react/goodbanners'
    })
    this.setData({
      banners: res.data.result
    })
  },
  parentClick() {
    console.log('parentClick - 父元素的事件 ')
  },
  handerClickOne() {
    console.log(' bind 不 会阻止事件向上冒泡。 ')
  },
  handerClickTwo() {
    console.log('catch 会阻止事件向上冒泡 ')
  },

  changeCount(e) {
    console.log(e)
    this.setData({
      count: this.data.count + e.target.dataset.count
    })
  },
  changeFlag() {
    this.setData({
      flag: !this.data.flag
    })
  },
  countAdd() {
    this.setData({
      count: ++this.data.count
    })
  },


  changeMsg() {
    this.setData({
      msg: 'WH2214 - 天道酬勤 '
    })
  },

  changeWord(e) {
    console.log(e) // e.detail.value 
    this.setData({
      word: e.detail.value
    })
  },

  getDataList() {
    // axios 
    wx.showLoading({
      title: '请求中...',
    })
    wx.request({
      //  url: 'http://121.196.235.163:3000/banner',
      url: 'http://localhost:3000/react/getcommentlist',
      method: "POST",
      data: {},
      success: res => {
        console.log(res)
        wx.showToast({
          title: '请求成功'
        })
      },
      fail() {

      },
      complete() {
        wx.hideLoading({
          success: (res) => { },
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getDataList()
    this.getbanners()
    this.getgoodsData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log("下拉刷新")
    wx.showLoading({
      title: '刷新中...'
    })
    setTimeout(() => {
      this.data.goods.reverse()
      this.setData({
        goods: this.data.goods
      })
      wx.hideLoading()
      wx.showToast({
        title: "刷新成功",
        icon: 'none'
      })
      wx.stopPullDownRefresh()
    }, 1000)
  },

  async getMoredata() {
    this.data, pageNo++;
    console.log(this.data.pageNo)
    if (this.data, pageNo > Math.ceil(this.data.total / this.data.pageSize)) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    } else {
      let res = await request({
        url: '/react/getgoods',
        data: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize,
        }
      })
      this.setData({
        goods: [...this.data.goods, ...res.data.result]
      })
      wx.showToast({
        title: "加载数据成功",
        icon: 'none'
      })
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log("触底了")
    this.getMoredata()
  },
  onPageScroll(e) {
    console.log('滚动了。。。')
    console.log(e)
    this.setData({
      headShow: !!(e.scrollTop > 255)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log("分享")
    return {
      title: 'WH2214-新年快乐',
      path: '/pages/home/home',
      imageUrl: this.data.links
    }
  },

  onTabItemTap(item) {
    console.log(item.index)
    console.log(item.pagePath)
    console.log(item.text)
  }
})